﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.HVAC.Nozzle;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.HVAC
{
	public class SideTerminalConnDuct
	{
		public SideTerminalConnDuct()
		{
		}

		public SideTerminalConnDuct(UIDocument uiDoc, ILayForm iLayForm, FamilySymbol familySymbol)
		{
			this.uiDocument = uiDoc;
			this.layForm = iLayForm;
			this.fSymbol = familySymbol;
			this.doc = this.uiDocument.Document;
		}

		public SideTerminalConnDuct(UIDocument uiDoc, FamilySymbol familySymbol, Nozzle nozzle, LaySideParameter laySidePara)
		{
			this.uiDocument = uiDoc;
			this.fSymbol = familySymbol;
			this.doc = this.uiDocument.Document;
			this.nozzle = nozzle;
			this.laySidePara = laySidePara;
		}

		public void ConnSingleSideTerminal()
		{
			AirTerminalParamNew airTerminalParaNew = this.layForm.airTerminalParaNew;
			int connType = -1;
			string text = "";
			PlanarFace planarFaceBySelectDuct = this.GetPlanarFaceBySelectDuct(this.uiDocument, this.layForm.SideTerminalWidth, ref this.reference, ref connType, "请选择侧风口布置位置...", true);
			if (planarFaceBySelectDuct == null)
			{
				YJKMessageBox.Information("未找到布置平面！");
				return;
			}
			Duct duct = this.doc.GetElementByRef(this.reference) as Duct;
			XYZ xyz = null;
			XYZ connPointOnDuct = ConnectHelper.GetConnPointOnDuct(duct, this.reference.GlobalPoint, planarFaceBySelectDuct, connType, ref xyz);
			double distance = new YJKPiping<Duct>(duct).GetWeightOrDiameter() * 2.5 * 304.8;
			XYZ layPoint = ConnectHelper.CalLayPoint(connPointOnDuct, xyz, distance);
			FamilyInstance familyInstance = this.LayFamilySymbol(layPoint, this.fSymbol, this.doc);
			this.SetFamilyInstanceParam(familyInstance, airTerminalParaNew);
			HYDuctConnect hyductConnect = new HYDuctConnect();
			hyductConnect.RotateFamilyInstance(familyInstance, xyz, connPointOnDuct);
			hyductConnect.DuctConnSideTerminalTakeOff(duct, familyInstance, ref text, connType, false);
		}

		public void ConnSingleNozzle()
		{
			int connType = -1;
			string text = "";
			PlanarFace planarFaceBySelectDuct = this.GetPlanarFaceBySelectDuct(this.uiDocument, this.nozzle.FengKouDn / 609.6, ref this.reference, ref connType, "请选择喷口布置位置...", true);
			if (planarFaceBySelectDuct == null)
			{
				YJKMessageBox.Information("没有选中布置面");
				return;
			}
			Duct duct = this.doc.GetElementByRef(this.reference) as Duct;
			XYZ xyz = null;
			XYZ connPointOnDuct = ConnectHelper.GetConnPointOnDuct(duct, this.reference.GlobalPoint, planarFaceBySelectDuct, connType, ref xyz);
			double distance = new YJKPiping<Duct>(duct).GetWeightOrDiameter() * 2.5 * 304.8;
			XYZ xyz2 = ConnectHelper.CalLayPoint(connPointOnDuct, xyz, distance);
			FamilyInstance familyInstance = this.LayFamilySymbol(xyz2, this.fSymbol, this.doc);
			this.SetNozzleFamilyInstancePara(familyInstance, this.nozzle);
			Connector elementConnector = YJKRevitTools.GetElementConnector(1, familyInstance);
			if (elementConnector != null)
			{
				YJKRotateFamilyInstance hyrotateFamilyInstance = new YJKRotateFamilyInstance();
				XYZ moveVector = xyz2.Subtract(elementConnector.Origin);
				hyrotateFamilyInstance.MoveElem(moveVector, familyInstance);
			}
			HYDuctConnect hyductConnect = new HYDuctConnect();
			hyductConnect.RotateFamilyInstance(familyInstance, xyz, connPointOnDuct);
			CylindricalNozzle cylindricalNozzle = this.nozzle as CylindricalNozzle;
			hyductConnect.DuctConnSideTerminalTakeOff(duct, familyInstance, ref text, connType, true);
		}

		public void ConnAlongLineSideNozzle()
		{
			int num = -1;
			PlanarFace planarFaceBySelectDuct = this.GetPlanarFaceBySelectDuct(this.uiDocument, this.nozzle.FengKouDn / 609.6, ref this.reference, ref num, "请选择喷口布置位置...", false);
			if (null == planarFaceBySelectDuct)
			{
				YJKMessageBox.Information("没有选中布置面!");
				return;
			}
			string text = "请选择喷口依次布置的方向点……";
			XYZ pickPoint = this.uiDocument.Selection.PickPoint((Autodesk.Revit.UI.Selection.ObjectSnapTypes)512, text);
			Duct duct = this.doc.GetElementByRef(this.reference) as Duct;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ xyz = null;
			XYZ connPointOnDuct = ConnectHelper.GetConnPointOnDuct(duct, this.reference.GlobalPoint, planarFaceBySelectDuct, num, ref xyz);
			Connector[] firstDuctInOutConnector = this.GetFirstDuctInOutConnector(hypiping, pickPoint, connPointOnDuct);
			SearchBranch searchBranch = new SearchBranch(new ElementNode(hypiping.Piping, firstDuctInOutConnector[0]));
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			XYZ ductVec = listRoot.ConnectorOut.Origin.Subtract(listRoot.ConnectorIn.Origin).Normalize();
			List<ElementNode> list = new List<ElementNode>();
			this.GetConnDuctList(listRoot, ref list, ductVec);
			YJKLine layTerminalLine = this.GetLayTerminalLine(connPointOnDuct, list.Last<ElementNode>());
			List<XYZ> list2 = this.CalculateLayPointsNozzle(layTerminalLine);
			Dictionary<Duct, List<XYZ>> dictionaryLayPointsToDuct = this.GetDictionaryLayPointsToDuct(list, list2);
			int num2 = this.LayAndConnNozzleToDuct(dictionaryLayPointsToDuct, planarFaceBySelectDuct, num);
			int num3 = list2.Count - num2;
			if (num3 != 0)
			{
				YJKMessageBox.Show("已删除不符合贴管连接喷口" + num3 + "个");
			}
		}

		public void ConnAlongLineSideTerminal()
		{
			AirTerminalParamNew airTerminalParaNew = this.layForm.airTerminalParaNew;
			int num = -1;
			PlanarFace planarFaceBySelectDuct = this.GetPlanarFaceBySelectDuct(this.uiDocument, this.layForm.SideTerminalWidth, ref this.reference, ref num, "请选择侧风口布置位置...", false);
			if (null == planarFaceBySelectDuct)
			{
				YJKMessageBox.Information("没有选中布置面");
				return;
			}
			string text = "请选择风口依次布置的方向点……";
			XYZ pickPoint = this.uiDocument.Selection.PickPoint((Autodesk.Revit.UI.Selection.ObjectSnapTypes)512, text);
			Duct duct = this.doc.GetElementByRef(this.reference) as Duct;
			YJKPiping<Duct> hypiping = new YJKPiping<Duct>(duct);
			XYZ xyz = null;
			XYZ connPointOnDuct = ConnectHelper.GetConnPointOnDuct(duct, this.reference.GlobalPoint, planarFaceBySelectDuct, num, ref xyz);
			Connector[] firstDuctInOutConnector = this.GetFirstDuctInOutConnector(hypiping, pickPoint, connPointOnDuct);
			SearchBranch searchBranch = new SearchBranch(new ElementNode(hypiping.Piping, firstDuctInOutConnector[0]));
			searchBranch.DoSearch();
			ElementNode listRoot = searchBranch.GetListRoot();
			XYZ ductVec = listRoot.ConnectorOut.Origin.Subtract(listRoot.ConnectorIn.Origin).Normalize();
			List<ElementNode> list = new List<ElementNode>();
			this.GetConnDuctList(listRoot, ref list, ductVec);
			YJKLine layTerminalLine = this.GetLayTerminalLine(connPointOnDuct, list.Last<ElementNode>());
			List<XYZ> list2 = this.CalculateLayPoints(layTerminalLine);
			Dictionary<Duct, List<XYZ>> dictionaryLayPointsToDuct = this.GetDictionaryLayPointsToDuct(list, list2);
			int num2 = this.LayAndConnTerminalsToDuct(dictionaryLayPointsToDuct, planarFaceBySelectDuct, num);
			int num3 = list2.Count - num2;
			if (num3 != 0)
			{
				YJKMessageBox.Show("已删除不符合贴管连接风口" + num3 + "个");
			}
		}

		private int LayAndConnTerminalsToDuct(Dictionary<Duct, List<XYZ>> dicDuctToPoints, PlanarFace pFace, int laySideType)
		{
			int num = 0;
			XYZ xyz = pFace.FaceNormal();
			foreach (KeyValuePair<Duct, List<XYZ>> keyValuePair in dicDuctToPoints)
			{
				if (keyValuePair.Value != null && keyValuePair.Value.Count >= 1)
				{
					Duct key = keyValuePair.Key;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(key);
					string text = "";
					foreach (XYZ point in keyValuePair.Value)
					{
						XYZ project = hypiping.GetProject(point);
						double distance = hypiping.GetWeightOrDiameter() * 2.5 * 304.8;
						if (this.BeOnDuctLine(hypiping, project, this.layForm.SideTerminalWidth))
						{
							num++;
							XYZ layPoint = ConnectHelper.CalLayPoint(project, xyz, distance);
							FamilyInstance familyInstance = this.LayFamilySymbol(layPoint, this.fSymbol, this.doc);
							AirTerminalParamNew airTerminalParaNew = this.layForm.airTerminalParaNew;
							this.SetFamilyInstanceParam(familyInstance, airTerminalParaNew);
							HYDuctConnect hyductConnect = new HYDuctConnect();
							hyductConnect.RotateFamilyInstance(familyInstance, xyz, project);
							hyductConnect.DuctConnSideTerminalTakeOff(key, familyInstance, ref text, laySideType, false);
						}
					}
				}
			}
			return num;
		}

		private int LayAndConnNozzleToDuct(Dictionary<Duct, List<XYZ>> dicDuctToPoints, PlanarFace pFace, int laySideType)
		{
			int num = 0;
			XYZ xyz = pFace.FaceNormal();
			foreach (KeyValuePair<Duct, List<XYZ>> keyValuePair in dicDuctToPoints)
			{
				if (keyValuePair.Value != null && keyValuePair.Value.Count >= 1)
				{
					Duct key = keyValuePair.Key;
					YJKPiping<Duct> hypiping = new YJKPiping<Duct>(key);
					string text = "";
					foreach (XYZ point in keyValuePair.Value)
					{
						XYZ project = hypiping.GetProject(point);
						double distance = hypiping.GetWeightOrDiameter() * 2.5 * 304.8;
						if (this.BeOnDuctLine(hypiping, project, this.nozzle.FengKouDn / 609.6))
						{
							num++;
							XYZ layPoint = ConnectHelper.CalLayPoint(project, xyz, distance);
							FamilyInstance familyInstance = this.LayFamilySymbol(layPoint, this.fSymbol, this.doc);
							this.SetNozzleFamilyInstancePara(familyInstance, this.nozzle);
							HYDuctConnect hyductConnect = new HYDuctConnect();
							hyductConnect.RotateFamilyInstance(familyInstance, xyz, project);
							hyductConnect.DuctConnSideTerminalTakeOff(key, familyInstance, ref text, laySideType, true);
						}
					}
				}
			}
			return num;
		}

		private bool BeOnDuctLine(YJKPiping<Duct> hyDuct, XYZ point, double devesion)
		{
			XYZ origin = hyDuct.StartConnector.Origin;
			XYZ origin2 = hyDuct.EndConnector.Origin;
			YJKLine hyline = new YJKLine(origin, origin2);
			XYZ project = hyline.GetProject(point);
			if (hyline.IsPointOnLineExtension(project))
			{
				return false;
			}
			double num = origin.DistanceTo(project);
			double num2 = origin2.DistanceTo(project);
			devesion /= 304.8;
			return num >= devesion && num2 >= devesion;
		}

		private Dictionary<Duct, List<XYZ>> GetDictionaryLayPointsToDuct(List<ElementNode> connNodes, List<XYZ> layPoints)
		{
			Dictionary<Duct, List<XYZ>> dictionary = new Dictionary<Duct, List<XYZ>>();
			if (connNodes == null || connNodes.Count < 1 || layPoints == null || layPoints.Count < 1)
			{
				return null;
			}
			foreach (ElementNode elementNode in connNodes)
			{
				List<XYZ> pointsOnDuct = this.GetPointsOnDuct(elementNode, ref layPoints);
				Duct duct = elementNode.Element as Duct;
				if (duct != null && pointsOnDuct != null)
				{
					dictionary.Add(duct, pointsOnDuct);
				}
			}
			return dictionary;
		}

		private List<XYZ> GetPointsOnDuct(ElementNode elemNode, ref List<XYZ> layPoints)
		{
			List<XYZ> list = new List<XYZ>();
			YJKLine hyline = new YJKLine(elemNode.ConnectorIn.Origin, elemNode.ConnectorOut.Origin);
			hyline.MakeUnBound();
			List<XYZ> list2 = new List<XYZ>();
			if (layPoints == null || layPoints.Count < 1)
			{
				return null;
			}
			foreach (XYZ xyz in layPoints)
			{
				XYZ project = hyline.GetProject(xyz);
				if (hyline.IsPointOnLineExtension(project))
				{
					list2.Add(xyz);
				}
				else
				{
					list.Add(xyz);
				}
			}
			layPoints = list2;
			return list;
		}

		private List<XYZ> CalculateLayPoints(YJKLine hyLine)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = hyLine.EndPoint.Subtract(hyLine.StartPoint).Normalize();
			list.Add(hyLine.StartPoint);
			int num = 1;
			double num2 = 0.0001;
			XYZ xyz2 = hyLine.StartPoint;
			double num3 = (double)this.layForm.laySideParam.Distance / 304.8;
			while (num < 10000 && (!this.layForm.laySideParam.BeNum || num < this.layForm.laySideParam.UpDownValue))
			{
				num++;
				xyz2 += xyz * num3;
				if (!hyLine.EndPoint.Subtract(xyz2).Normalize().IsAlmostEqualTo(xyz, num2))
				{
					break;
				}
				list.Add(xyz2);
			}
			return list;
		}

		private List<XYZ> CalculateLayPointsNozzle(YJKLine hyLine)
		{
			List<XYZ> list = new List<XYZ>();
			XYZ xyz = hyLine.EndPoint.Subtract(hyLine.StartPoint).Normalize();
			list.Add(hyLine.StartPoint);
			int num = 1;
			double num2 = 0.0001;
			XYZ xyz2 = hyLine.StartPoint;
			double num3 = (double)this.laySidePara.Distance / 304.8;
			while (num < 10000 && (!this.laySidePara.BeNum || num < this.laySidePara.UpDownValue))
			{
				num++;
				xyz2 += xyz * num3;
				if (!hyLine.EndPoint.Subtract(xyz2).Normalize().IsAlmostEqualTo(xyz, num2))
				{
					break;
				}
				list.Add(xyz2);
			}
			return list;
		}

		private YJKLine GetLayTerminalLine(XYZ startPoint, ElementNode lastNode)
		{
			XYZ origin = lastNode.ConnectorOut.Origin;
			XYZ origin2 = lastNode.ConnectorIn.Origin;
			XYZ xyz = origin.Subtract(origin2).Normalize();
			YJKLine hyline = new YJKLine(startPoint, startPoint.Add(xyz));
			hyline.MakeUnBound();
			XYZ project = hyline.GetProject(origin);
			return new YJKLine(startPoint, project);
		}

		private void GetConnDuctList(ElementNode rootNode, ref List<ElementNode> connNodes, XYZ ductVec)
		{
			if (rootNode == null)
			{
				return;
			}
			bool flag = rootNode.Element is Duct;
			double num = 0.0001;
			if (flag)
			{
				if (!rootNode.ConnectorOut.Origin.Subtract(rootNode.ConnectorIn.Origin).Normalize().IsAlmostEqualTo(ductVec, num))
				{
					return;
				}
				connNodes.Add(rootNode);
			}
			this.GetConnDuctList(rootNode.Next, ref connNodes, ductVec);
			if (rootNode.SubElementNode != null)
			{
				foreach (ElementNode rootNode2 in rootNode.SubElementNode)
				{
					this.GetConnDuctList(rootNode2, ref connNodes, ductVec);
				}
			}
		}

		private Connector[] GetFirstDuctInOutConnector(YJKPiping<Duct> hyDuct, XYZ pickPoint, XYZ basePnt)
		{
			Connector[] array = new Connector[2];
			Connector connector = hyDuct.StartConnector.Connector;
			Connector connector2 = hyDuct.EndConnector.Connector;
			YJKLine hyline = new YJKLine(connector.Origin, connector2.Origin);
			hyline.MakeUnBound();
			XYZ xyz = hyline.GetProject(pickPoint).Subtract(basePnt).Normalize();
			XYZ xyz2 = connector.Origin.Subtract(connector2.Origin).Normalize();
			if (xyz.IsAlmostEqualTo(xyz2, 0.0001))
			{
				array[0] = connector2;
				array[1] = connector;
			}
			else
			{
				array[0] = connector;
				array[1] = connector2;
			}
			return array;
		}

		public PlanarFace GetPlanarFaceBySelectDuct(UIDocument uiDoc, double devision, ref Reference refer, ref int connType, string prompt, bool beInEnd = true)
		{
			Duct duct = null;
			YJKPiping<Duct> hypiping = null;
			while (duct == null)
			{
				try
				{
					refer = uiDoc.Selection.PickObject((Autodesk.Revit.UI.Selection.ObjectType)2, prompt);
				}
				catch (Autodesk.Revit.Exceptions.OperationCanceledException ex)
				{
					throw ex;
				}
				catch (Exception ex2)
				{
					throw ex2;
				}
				duct = (uiDoc.Document.GetElement(refer) as Duct);
				if (duct == null)
				{
					YJKMessageBox.Information("请选择风管！");
				}
				else
				{
					hypiping = new YJKPiping<Duct>(duct);
					if (hypiping.Shape == 1)
					{
						break;
					}
					YJKMessageBox.Information("侧壁连接侧风口，仅支持矩形风管，\n请选择矩形风管！");
					duct = null;
				}
			}
			return hypiping.GetDuctConnFace(refer.GlobalPoint, devision, ref connType, beInEnd);
		}

		public bool CheckRoom(Element roomElement)
		{
			IEnumerable<IList<BoundarySegment>> boundarySegments = (roomElement as SpatialElement).GetBoundarySegments(new SpatialElementBoundaryOptions
			{
				SpatialElementBoundaryLocation = 0
			});
			List<BoundarySegment> list = new List<BoundarySegment>();
			foreach (IList<BoundarySegment> list2 in boundarySegments)
			{
				List<BoundarySegment> collection = (List<BoundarySegment>)list2;
				list.AddRange(collection);
			}
			return list.Count == 4;
		}

		public bool CheckReferencePlaneName(Autodesk.Revit.DB.Document doc, string name)
		{
			foreach (Element element in ((IEnumerable<Element>)YJKElementFilter.GetElementByCategory(doc, BuiltInCategory.OST_CLines)))
			{
				if (element is ReferencePlane && (element as ReferencePlane).Name == name)
				{
					return true;
				}
			}
			return false;
		}

		public void SettingParameter(Autodesk.Revit.DB.Document doc, ElementSet es, ICollection<Element> collectionId2, AirTerminalParamNew airTerminalPara, bool isSetLevel, int uni, FamilySymbol fs)
		{
			Element element = null;
			Element element2 = null;
			bool flag = true;
			foreach (object obj in es)
			{
				Element element3 = (Element)obj;
				if (element3 != null)
				{
					XYZ point = (element3.Location as LocationPoint).Point;
					element = element3;
					bool flag2 = false;
					foreach (Element element4 in collectionId2)
					{
						if (((IndependentTag)element4).TaggedLocalElementId.IntegerValue == element3.Id.IntegerValue)
						{
							flag2 = true;
							element2 = element4;
							collectionId2.Remove(element4);
							break;
						}
					}
					if (!flag2 && airTerminalPara.Tag && flag)
					{
						try
						{
							RevitVersionFuncs.NewTag(doc, doc.ActiveView, new Reference(element), false, 0, 0, point);
							goto IL_F7;
						}
						catch (Exception)
						{
							flag = false;
							MessageBox.Show("标注失败，可能原因是项目里没有标记族，请手动加载并配置", "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
							goto IL_F7;
						}
						goto IL_DD;
					}
					goto IL_DD;
					IL_F7:
					this.SetFamilyInstanceParam(element, airTerminalPara);
					if (isSetLevel)
					{
						Parameter parameter = null;
						Parameter parameter2 = null;
						double num = 0.0;
						if (element.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM) != null)
						{
							parameter2 = element.GetParameter(BuiltInParameter.INSTANCE_ELEVATION_PARAM);
						}
						Parameter parameter3 = element.GetParameter(BuiltInParameter.FAMILY_LEVEL_PARAM);
						if (parameter3 != null && !parameter3.IsReadOnly)
						{
							parameter3.Set(doc.ActiveView.GenLevel.Id);
						}
						if (parameter2 != null)
						{
							parameter2.Set(UnitConvert.CovertToAPI(airTerminalPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
							num = parameter2.AsDouble();
						}
						if (num != UnitConvert.CovertToAPI(airTerminalPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2))
						{
							if (element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM) != null)
							{
								parameter = element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
							}
							if (parameter != null)
							{
								parameter.Set(UnitConvert.CovertToAPI(airTerminalPara.OffSet * 1000.0, (Autodesk.Revit.DB.DisplayUnitType)2));
							}
						}
					}
					else if (uni == 1)
					{
						Parameter parameter4 = null;
						if (element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM) != null)
						{
							parameter4 = element.GetParameter(BuiltInParameter.INSTANCE_FREE_HOST_OFFSET_PARAM);
						}
						if (parameter4 != null)
						{
							parameter4.Set(0);
						}
					}
					doc.Regenerate();
					continue;
					IL_DD:
					if (flag2 && !airTerminalPara.Tag)
					{
						doc.Delete(element2.Id);
						goto IL_F7;
					}
					goto IL_F7;
				}
			}
		}

		private bool IsHYFamily(string familyName)
		{
			return new List<string>
			{
				"方形风口",
				"圆形风口",
				"方形散流器",
				"矩形风口",
				"条缝型风口",
				"圆形散流器",
				"侧风口"
			}.Find((string p) => p == familyName) != null;
		}

		public FamilyInstance LayFamilySymbol(XYZ layPoint, FamilySymbol fs, Autodesk.Revit.DB.Document doc)
		{
			UnifiedModified.ActivateFamilySymbol(fs);
			FamilyInstance result = doc.Create.NewFamilyInstance(layPoint, fs, (Autodesk.Revit.DB.Structure.StructuralType)0);
			doc.Regenerate();
			return result;
		}

		private void SetNozzleFamilyInstancePara(Element elem, Nozzle nozzle)
		{
			if (nozzle is GlobeNozzle)
			{
				GlobeNozzle globeNozzle = nozzle as GlobeNozzle;
				Parameter parameter = elem.GetParameter("末端气流速度");
				if (parameter != null && !parameter.IsReadOnly)
				{
					parameter.Set(UnitConvert.CovertToAPI(globeNozzle.FengSu, (Autodesk.Revit.DB.DisplayUnitType)61));
				}
				Parameter parameter2 = elem.GetParameter("规格");
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(globeNozzle.GuiGe);
				}
				Parameter parameter3 = elem.GetParameter("射程(m)");
				if (parameter3 != null && !parameter3.IsReadOnly)
				{
					parameter3.Set(globeNozzle.SheCheng.ToString());
				}
				Parameter parameter4 = elem.GetParameter("风量");
				if (parameter4 != null && !parameter4.IsReadOnly)
				{
					parameter4.Set(UnitConvert.CovertToAPI((double)globeNozzle.FengLiang, (Autodesk.Revit.DB.DisplayUnitType)66));
				}
				using (List<string>.Enumerator enumerator = new List<string>
				{
					"D1",
					"D2",
					"D3",
					"D4",
					"E",
					"F",
					"L1",
					"L2"
				}.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string text = enumerator.Current;
						Parameter parameter5 = elem.GetParameter(text);
						if (parameter5 != null && !parameter5.IsReadOnly)
						{
							double value = (double)globeNozzle.GetType().GetProperty(text).GetValue(globeNozzle, null);
							parameter5.Set(UnitConvert.CovertToAPI(value, (Autodesk.Revit.DB.DisplayUnitType)2));
						}
					}
					return;
				}
			}
			if (nozzle is CylindricalNozzle)
			{
				CylindricalNozzle cylindricalNozzle = nozzle as CylindricalNozzle;
				Parameter parameter6 = elem.GetParameter("纵向气流落差(m)");
				if (parameter6 != null && !parameter6.IsReadOnly)
				{
					parameter6.Set(cylindricalNozzle.QiLiu.ToString());
				}
				Parameter parameter7 = elem.GetParameter("规格");
				if (parameter7 != null && !parameter7.IsReadOnly)
				{
					parameter7.Set(cylindricalNozzle.GuiGe);
				}
				Parameter parameter8 = elem.GetParameter("射程(m)");
				if (parameter8 != null && !parameter8.IsReadOnly)
				{
					parameter8.Set(cylindricalNozzle.SheCheng.ToString());
				}
				Parameter parameter9 = elem.GetParameter("风量");
				if (parameter9 != null && !parameter9.IsReadOnly)
				{
					parameter9.Set(UnitConvert.CovertToAPI((double)cylindricalNozzle.FengLiang, (Autodesk.Revit.DB.DisplayUnitType)66));
				}
				foreach (string text2 in new List<string>
				{
					"C",
					"L2",
					"L3",
					"L",
					"D",
					"E",
					"F",
					"B",
					"H"
				})
				{
					Parameter parameter10 = elem.GetParameter(text2);
					if (parameter10 != null && !parameter10.IsReadOnly)
					{
						double value2 = (double)cylindricalNozzle.GetType().GetProperty(text2).GetValue(cylindricalNozzle, null);
						parameter10.Set(UnitConvert.CovertToAPI(value2, (Autodesk.Revit.DB.DisplayUnitType)2));
					}
				}
			}
		}

		private void SetFamilyInstanceParam(Element elem, AirTerminalParamNew airTerminalPara)
		{
			Parameter parameter = elem.GetParameter("回风箭头可见性");
			Parameter parameter2 = elem.GetParameter("送风箭头可见性");
			string terminalType = airTerminalPara.TerminalType;
			if (!(terminalType == "in"))
			{
				if (!(terminalType == "out"))
				{
					if (terminalType == "none")
					{
						if (parameter != null && !parameter.IsReadOnly)
						{
							parameter.Set(0);
						}
						if (parameter2 != null && !parameter2.IsReadOnly)
						{
							parameter2.Set(0);
						}
					}
				}
				else
				{
					if (parameter != null && !parameter.IsReadOnly)
					{
						parameter.Set(0);
					}
					if (parameter2 != null && !parameter2.IsReadOnly)
					{
						parameter2.Set(1);
					}
				}
			}
			else
			{
				if (parameter != null && !parameter.IsReadOnly)
				{
					parameter.Set(1);
				}
				if (parameter2 != null && !parameter2.IsReadOnly)
				{
					parameter2.Set(0);
				}
			}
			if (!this.SetParameterValue(elem, "风口长度", airTerminalPara.TerminalWidth, 2))
			{
				this.SetParameterValue(elem, "风口宽度", airTerminalPara.TerminalWidth, 2);
				this.SetParameterValue(elem, "风口厚度", airTerminalPara.TerminalThick, 2);
				this.SetParameterValue(elem, "风口高度", airTerminalPara.TerminalHeight, 2);
			}
			else
			{
				this.SetParameterValue(elem, "风口宽度", airTerminalPara.TerminalHeight, 2);
				this.SetParameterValue(elem, "风口高度", airTerminalPara.TerminalThick, 2);
			}
			this.SetParameterValue(elem, "风口直径", airTerminalPara.TerminalDiameter, 2);
			this.SetParameterValue(elem, "喉口宽度", airTerminalPara.ThroatWidth, 2);
			this.SetParameterValue(elem, "喉口高度", airTerminalPara.ThroatHeight, 2);
			this.SetParameterValue(elem, "喉口直径", airTerminalPara.ThroatDiameter, 2);
			this.SetParameterValue(elem, "流量", airTerminalPara.Flow * 5.0 / 18.0, 64);
			this.SetParameterValue(elem, "风量", airTerminalPara.Flow * 5.0 / 18.0, 64);
			this.SetParameterValue(elem, "速度", airTerminalPara.Speed, 61);
			this.SetParameterValue(elem, "喉口风速", airTerminalPara.Speed, 61);
			this.SetParameterValue(elem, "阻力损失", airTerminalPara.Drop, 48);
			this.SetParameterValue(elem, "局部阻力", airTerminalPara.Drop, 48);
		}

		private bool SetParameterValue(Element elem, string paramName, double paramValue, DisplayUnitType unitType)
		{
			Parameter parameter = elem.GetParameter(paramName);
			if (parameter == null)
			{
				return false;
			}
			if (parameter.IsReadOnly)
			{
				return true;
			}
			parameter.Set(UnitConvert.CovertToAPI(paramValue, unitType));
			return true;
		}

		public List<XYZ> GetLayGridXYZList(IList<Element> elemList, double offSet)
		{
			List<XYZ> list = new List<XYZ>();
			IList<HYBoostCurve> list2 = new List<HYBoostCurve>();
			list2 = this.ConvertElementCurveToCurve(elemList);
			foreach (HYBoostCurve hyboostCurve in list2)
			{
				HYBoostCurve hyboostCurve2 = hyboostCurve;
				foreach (HYBoostCurve hyboostCurve3 in list2)
				{
					if (hyboostCurve.Id != hyboostCurve3.Id)
					{
						List<XYZ> list3 = hyboostCurve2.IntersectionAllPoint(hyboostCurve3.Curve);
						if (list3 != null)
						{
							foreach (XYZ xyz in list3)
							{
								XYZ xyz2 = new XYZ(xyz.X, xyz.Y, offSet);
								if (list.Count < 1)
								{
									list.Add(xyz2);
								}
								else
								{
									bool flag = false;
									foreach (XYZ xyz3 in list)
									{
										if (xyz2.IsAlmostEqualTo(xyz3))
										{
											flag = true;
											break;
										}
									}
									if (!flag)
									{
										list.Add(xyz2);
									}
								}
							}
						}
					}
				}
			}
			return list;
		}

		private IList<HYBoostCurve> ConvertElementCurveToCurve(IList<Element> elemList)
		{
			IList<HYBoostCurve> list = new List<HYBoostCurve>();
			foreach (Element element in elemList)
			{
				HYBoostCurve hyboostCurve = null;
				if (element.Location is LocationCurve)
				{
					hyboostCurve = new HYBoostCurve((element.Location as LocationCurve).Curve, element.Id.IntegerValue);
				}
				else if (element is ReferencePlane)
				{
					ReferencePlane referencePlane = element as ReferencePlane;
					hyboostCurve = new HYBoostCurve(Line.CreateBound(referencePlane.BubbleEnd, referencePlane.FreeEnd), element.Id.IntegerValue);
				}
				if (hyboostCurve != null)
				{
					list.Add(hyboostCurve);
				}
			}
			return list;
		}

		public void DrawRectangular(Autodesk.Revit.DB.Document doc, XYZ centreArgu, double widthArgu, double lengthArgu, double angleArgu)
		{
			centreArgu = new XYZ(centreArgu.X, centreArgu.Y, 0.0);
			XYZ xyz = new XYZ(centreArgu.X - lengthArgu / 2.0, centreArgu.Y + widthArgu / 2.0, 0.0);
			XYZ xyz2 = new XYZ(centreArgu.X - lengthArgu / 2.0, centreArgu.Y - widthArgu / 2.0, 0.0);
			XYZ xyz3 = new XYZ(centreArgu.X + lengthArgu / 2.0, centreArgu.Y + widthArgu / 2.0, 0.0);
			XYZ xyz4 = new XYZ(centreArgu.X + lengthArgu / 2.0, centreArgu.Y - widthArgu / 2.0, 0.0);
			Line item = Line.CreateBound(xyz, xyz2);
			Line item2 = Line.CreateBound(xyz, xyz3);
			Line item3 = Line.CreateBound(xyz2, xyz4);
			Line item4 = Line.CreateBound(xyz3, xyz4);
			foreach (Line line in ((IEnumerable<Line>)new List<Line>
			{
				item,
				item2,
				item3,
				item4
			}))
			{
				Element element = doc.Create.NewDetailCurve(doc.ActiveView, line) as DetailLine;
				Line line2 = Line.CreateBound(centreArgu, centreArgu + new XYZ(0.0, 0.0, 1.0));
				element.Location.Rotate(line2, angleArgu / 180.0 * Math.PI);
			}
		}

		private LaySideParameter laySidePara;

		private Nozzle nozzle;

		private UIDocument uiDocument;

		private Reference reference;

		private ILayForm layForm;

		private FamilySymbol fSymbol;

		private Document doc;
	}
}
